package com.inepex.inechart.shared.axes;

import java.util.ArrayList;
import java.util.TreeMap;

import com.inepex.inechart.client.Defaults;
import com.inepex.inechart.client.IneChart;
import com.inepex.inechart.client.label.LabelFactory;
import com.inepex.inechart.client.label.StyledLabel;
import com.inepex.inechart.shared.CartesianAssist;
import com.inepex.inegraphics.impl.client.TextPositionerBase;
import com.inepex.inegraphics.shared.DrawingArea;
import com.inepex.inegraphics.shared.DrawingAreaAssist;
import com.inepex.inegraphics.shared.GraphicalObjectContainer;
import com.inepex.inegraphics.shared.gobjects.Line;
import com.inepex.inegraphics.shared.gobjects.Text;

/**
 * 
 * Module to calculate and display {@link Axis} objects.
 * 
 * Should be singleton per {@link IneChart}.
 * 
 * @author Miklós Süveges, Tibor Somodi / Inepex Ltd.
 * 
 */
public class AxesDefault implements Axes{
	private final LabelFactory labelFactory;
	private final DrawingArea drawingArea;
	private final TickFactory tickFactory;
	private final AxisToGO axisToGO;

	private ArrayList<Axis> axes;
	private TreeMap<Axis, GraphicalObjectContainer> gosPerAxis;
	private TreeMap<Axis, ArrayList<StyledLabel>> labelsPerAxis;
	private TreeMap<Tick, Text> textMap;
	private TreeMap<Axis, double[]> paddingAroundAxes;
	private TreeMap<Axis, double[]> boundingBoxes;

	public AxesDefault(TickFactory tickFactory, LabelFactory labelFactory, DrawingArea drawingArea) {
		axes = new ArrayList<Axis>();
		gosPerAxis = new TreeMap<Axis, GraphicalObjectContainer>();
		labelsPerAxis = new TreeMap<Axis, ArrayList<StyledLabel>>();
		paddingAroundAxes = new TreeMap<Axis, double[]>();
		boundingBoxes = new TreeMap<Axis, double[]>();
		textMap = new TreeMap<Tick, Text>();
		this.labelFactory = labelFactory;
		this.tickFactory = tickFactory;
		this.drawingArea = drawingArea;
		this.axisToGO = new AxisToGO(tickFactory, drawingArea);
	}

	public void addAxis(Axis axis) {
		//		if(moduleAssist.isClientSide()){
		//			if(axes.contains(axis.getPerpendicularAxis())){
		//				canvasPerAxis.put(axis, canvasPerAxis.get(axis.getPerpendicularAxis()));
		//			}
		//			else{
		//				Layer axisLayer = moduleAssist.createAndAttachLayer(Layer.ALWAYS_BOT);
		//				axis.modulToAlign.getModuleLayer().addLayer(axisLayer);
		//				canvasPerAxis.put(axis, axisLayer.getCanvas());
		//			}
		//		}
		//		else{
		//			canvasPerAxis.put(axis, canvas);
		//		}		
		axes.add(axis);
	}

	public void removeAxis(Axis axis) {
		removeAllGOAndLabelRelatedToAxis(axis);
		gosPerAxis.remove(axis);
		labelsPerAxis.remove(axis);
		axes.remove(axis);
	}


	public void update() {
		for (Axis axis : axes) {
			removeAllGOAndLabelRelatedToAxis(axis);
			if (!axis.isVisible){
				continue;
			}
			setTicksIf(axis);
			createGOsAndLabelsForAxis(axis);
		}
	}

	private void setTicksIf(Axis axis){
		if (axis.autoCreateTicks){
			// auto calc tick's positions and apply defaults
			tickFactory.autoCreateTicks(axis);
			createDefaultTickAndLabelForAxis(axis);
		}
		else if(axis.defaultTick != null){
			//apply default tick properties
			createDefaultTickAndLabelForAxis(axis);
		}
	}

	public void updateForPaddingCalculation(){
		for (Axis axis : axes) {
			if (!axis.isVisible){
				continue;
			}
			setTicksIf(axis);
			measurePaddingForAxis(axis);
		}
	}

	public void updateWithOutAutoTickCreation(){
		for (Axis axis : axes) {
			removeAllGOAndLabelRelatedToAxis(axis);
			if (!axis.isVisible){
				continue;
			}
			createGOsAndLabelsForAxis(axis);
		}
	}


	private void createGOsAndLabelsForAxis(Axis axis) {
		GraphicalObjectContainer goc = new GraphicalObjectContainer();
		double startX = 0, startY = 0, endX = 0, endY = 0;
		Axis perpAxis = axis.getPerpendicularAxis();
		// determine the axis' position
		CartesianAssist cartesianAssist =  axis.getModulToAlign().getCartesianAssist();
		switch (axis.axisPosition) {
		case Minimum:
			if (perpAxis.axisDirection == AxisDirection.Horizontal_Ascending_To_Left) {
				startX = endX = cartesianAssist.getChartWidth() - axis.modulToAlign.getCartesianAssist().getRightPadding();
			} else if (perpAxis.axisDirection == AxisDirection.Horizontal_Ascending_To_Right) {
				startX = endX = axis.modulToAlign.getLeftPadding();
			} else if (perpAxis.axisDirection == AxisDirection.Vertical_Ascending_To_Bottom) {
				startY = endY = axis.modulToAlign.getTopPadding();
			} else if (perpAxis.axisDirection == AxisDirection.Vertical_Ascending_To_Top) {
				startY = endY = cartesianAssist.getChartHeight() - axis.modulToAlign.getBottomPadding();
			}
			break;
		case Maximum:
			if (perpAxis.axisDirection == AxisDirection.Horizontal_Ascending_To_Right) {
				startX = endX = cartesianAssist.getChartWidth() - axis.modulToAlign.getRightPadding();
			} else if (perpAxis.axisDirection == AxisDirection.Horizontal_Ascending_To_Left) {
				startX = endX = axis.modulToAlign.getLeftPadding();
			} else if (perpAxis.axisDirection == AxisDirection.Vertical_Ascending_To_Top) {
				startY = endY = axis.modulToAlign.getTopPadding();
			} else if (perpAxis.axisDirection == AxisDirection.Vertical_Ascending_To_Bottom) {
				startY = endY = cartesianAssist.getChartHeight() - axis.modulToAlign.getBottomPadding();
			}
			break;
		case Middle:
			if (perpAxis.isHorizontal()) {
				startX = endX = cartesianAssist.getWidth() / 2 + axis.modulToAlign.getLeftPadding();
			} else {
				startY = endY = cartesianAssist.getHeight() / 2 + axis.modulToAlign.getTopPadding();
			}
			break;
		case Fixed:
			if (axis.fixedPosition >= perpAxis.min && axis.fixedPosition <= perpAxis.max) {
				if (perpAxis.isHorizontal()) {
					startX = endX = cartesianAssist.modelToCanvasHorizontal(axis.fixedPosition);
				} else {
					startY = endY = cartesianAssist.modelToCanvasVertical(axis.fixedPosition);
				}
			} else
				axis.isVisible = false;
			break;
		case Fixed_Dock_If_Not_Visible:
			double pos = axis.fixedPosition;
			if (axis.fixedPosition < perpAxis.min) {
				pos = perpAxis.min;
			} else if (axis.fixedPosition > perpAxis.max) {
				pos = perpAxis.max;
			}
			if (perpAxis.isHorizontal()) {
				startX = endX = cartesianAssist.modelToCanvasHorizontal(pos);
			} else {
				startY = endY = cartesianAssist.modelToCanvasVertical(pos);
			}
			break;
		}
		if (perpAxis.isHorizontal()) {
			if (axis.getAxisDirection() == AxisDirection.Vertical_Ascending_To_Bottom) {
				startY = axis.modulToAlign.getTopPadding();
				endY = cartesianAssist.getChartHeight() - axis.modulToAlign.getBottomPadding();
			} else if (axis.getAxisDirection() == AxisDirection.Vertical_Ascending_To_Top) {
				endY = axis.modulToAlign.getTopPadding();
				startY = cartesianAssist.getChartHeight() - axis.modulToAlign.getBottomPadding();
			}
		} else {
			if (axis.getAxisDirection() == AxisDirection.Horizontal_Ascending_To_Left) {
				startX = cartesianAssist.getChartWidth() - axis.modulToAlign.getRightPadding();
				endX = axis.modulToAlign.getLeftPadding();
			} else if (axis.getAxisDirection() == AxisDirection.Horizontal_Ascending_To_Right) {
				endX = cartesianAssist.getChartWidth() - axis.modulToAlign.getRightPadding();
				startX = axis.modulToAlign.getLeftPadding();
			}
		}

		axisToGO.createAxisLine(axis, goc, startX, startY, endX, endY);

		GraphicalObjectContainer gridGOs = new GraphicalObjectContainer();
		textMap.clear();
		axisToGO.createTickLinesLabelsGrids(axis, perpAxis, goc, gridGOs, startX, startY);

		axisToGO.createFillBetweenTicks(axis, gridGOs);

		//add axis line to get a valid bounding box
		GraphicalObjectContainer gocToMeasure = new GraphicalObjectContainer();
		gocToMeasure.add(goc);
		gocToMeasure.add(new Line(startX, startY, endX, endY, null, 0));
		//		//add the hacked text objects
		for(Tick t : textMap.keySet()){
			gocToMeasure.add(textMap.get(t));
		}

		double[] paddingAroundAxis = new double[]{0,0,0,0};
		double[] axisBB = DrawingAreaAssist.getBoundingBox(gocToMeasure);
		if(axis.isHorizontal()){
			paddingAroundAxis[0] = startY - axisBB[1];
			paddingAroundAxis[2] = axisBB[1] + axisBB[3] - startY;
			if (axis.getAxisDirection() == AxisDirection.Horizontal_Ascending_To_Left) {
				paddingAroundAxis[1] = axisBB[0] + axisBB[2] - startX;
				paddingAroundAxis[3] = endX - axisBB[0];
			}
			else if (axis.getAxisDirection() == AxisDirection.Horizontal_Ascending_To_Right) {
				paddingAroundAxis[1] = axisBB[0] + axisBB[2] - endX;
				paddingAroundAxis[3] = startX - axisBB[0];
			}
		}
		else{
			paddingAroundAxis[1] = axisBB[0] + axisBB[2] - startX;
			paddingAroundAxis[3] = startX - axisBB[0];
			if (axis.getAxisDirection() == AxisDirection.Vertical_Ascending_To_Bottom) {
				paddingAroundAxis[0] = startY - axisBB[1];
				paddingAroundAxis[2] = axisBB[1] + axisBB[3] - endY;
			}
			else if (axis.getAxisDirection() == AxisDirection.Vertical_Ascending_To_Top) {
				paddingAroundAxis[0] = endY - axisBB[1];
				paddingAroundAxis[2] = axisBB[1] + axisBB[3] - startY;
			}
		}
		labelsPerAxis.put(axis, new ArrayList<StyledLabel>());
		
		createAxisLabel(axis, perpAxis, axisBB, paddingAroundAxis, true);
		goc.add(gridGOs);
		paddingAroundAxes.put(axis, paddingAroundAxis);
		boundingBoxes.put(axis, axisBB);
		gosPerAxis.put(axis, goc);

		//replace Texts w styledlabel
		for(Tick t : textMap.keySet()){
			Text text = textMap.get(t);
			StyledLabel lbl = new StyledLabel(t.getText());
			TextPositionerBase.calcTextPosition(text);
			lbl.setLeft((int) text.getBasePointX());
			lbl.setTop((int) text.getBasePointY());
			lbl.copyLookoutProperties(t.textContainer);
			this.labelsPerAxis.get(axis).add(lbl);
			labelFactory.addAndDisplayStyledLabel(lbl);
		}

	}

	private void measurePaddingForAxis(Axis axis){
		double axisStartX = axis.getModulToAlign().getLeftPadding(), axisStartY = axis.getModulToAlign().getTopPadding();
		double axisEndX = axisStartX + (axis.isHorizontal() ? axis.getModulToAlign().getCartesianAssist().getWidth() : 0);
		double axisEndY = axisStartY + (axis.isHorizontal() ? 0 : axis.getModulToAlign().getCartesianAssist().getHeight());
		GraphicalObjectContainer gos = new GraphicalObjectContainer();
		gos.add(new Line(axisStartX, axisStartY, axisEndX, axisEndY, null, 0));
		textMap.clear();
		axisToGO.createTickLinesLabelsGrids(axis, axis.getPerpendicularAxis(), gos, null, axisStartX, axisStartY);
		for(Tick t : textMap.keySet()){
			gos.add(textMap.get(t));
		}

		double[] paddingAroundAxis = new double[]{0,0,0,0};
		double[] axisBB = DrawingAreaAssist.getBoundingBox(gos);
		if(axis.isHorizontal()){
			paddingAroundAxis[0] = axisStartY - axisBB[1];
			paddingAroundAxis[2] = axisBB[1] + axisBB[3] - axisStartY;
			if (axis.getAxisDirection() == AxisDirection.Horizontal_Ascending_To_Left) {
				paddingAroundAxis[1] = axisBB[0] + axisBB[2] - axisStartX;
				paddingAroundAxis[3] = axisEndX - axisBB[0];
			}
			else if (axis.getAxisDirection() == AxisDirection.Horizontal_Ascending_To_Right) {
				paddingAroundAxis[1] = axisBB[0] + axisBB[2] - axisEndX;
				paddingAroundAxis[3] = axisStartX - axisBB[0];
			}
		}
		else{
			paddingAroundAxis[1] = axisBB[0] + axisBB[2] - axisStartX;
			paddingAroundAxis[3] = axisStartX - axisBB[0];
			paddingAroundAxis[0] = axisStartY - axisBB[1];
			paddingAroundAxis[2] = axisBB[1] + axisBB[3] - axisEndY;
		}
		createAxisLabel(axis, axis.getPerpendicularAxis(), axisBB, paddingAroundAxis, false);

		paddingAroundAxes.put(axis, paddingAroundAxis);
		boundingBoxes.put(axis, axisBB);
	}

	private void createAxisLabel(Axis axis, Axis perpAxis, double[] boundingBox, double[] paddingAroundAxis, boolean display){
		if(axis.axisLabel == null || axis.axisLabel.getText().getText() == null || axis.axisLabel.getText().getText().length() == 0)
			return;
		Text text = new Text(axis.axisLabel.getText().getText(),0,0);
		text.setFontFamily(axis.axisLabel.getText().getTextProperties().getFontFamily());
		text.setFontStyle(axis.axisLabel.getText().getTextProperties().getFontStyle());
		text.setFontWeight(axis.axisLabel.getText().getTextProperties().getFontWeight());
		text.setColor(axis.axisLabel.getText().getTextProperties().getColor().getColor());
		text.setFontSize(axis.axisLabel.getText().getTextProperties().getFontSize());
		drawingArea.measureText(text);
		text.setWidth( (int) (text.getWidth() +
				axis.axisLabel.getLeftPadding() + axis.axisLabel.getRightPadding() +
				axis.axisLabel.getBackground().getLineProperties().getLineWidth() * 2) + 1);
		text.setHeight( (int) (text.getHeight() + 
				axis.axisLabel.getTopPadding() + axis.axisLabel.getBottomPadding() +
				axis.axisLabel.getBackground().getLineProperties().getLineWidth() * 2) + 1);
		int x = 0, y = 0;
		if (axis.isHorizontal()) {
			switch(axis.axisLabel.getHorizontalPosition()){
			case Auto:
			case Middle:
				x = (int) (boundingBox[0] + boundingBox[2] / 2) - text.getWidth() / 2;
				break;
			case Left:
				x = (int) boundingBox[0];
				break;
			case Right:
				x = (int) (boundingBox[0] + boundingBox[2] ) - text.getWidth();
				break;
			}
			switch(axis.axisLabel.getVerticalPosition()){
			case Auto:
				//at bot
				if (axis.getAxisPosition() == AxisPosition.Maximum && perpAxis.getAxisDirection() == AxisDirection.Vertical_Ascending_To_Bottom ||
				axis.getAxisPosition() == AxisPosition.Minimum && perpAxis.getAxisDirection() == AxisDirection.Vertical_Ascending_To_Top) {
					y = (int) (boundingBox[1] + boundingBox[3]);
					paddingAroundAxis[2] += text.getHeight();
				}
				else{
					y = (int) (boundingBox[1] - text.getHeight());
					paddingAroundAxis[0] += text.getHeight();
				}
				break;
			case Bottom:
				y = (int) (boundingBox[1] + boundingBox[3]);
				paddingAroundAxis[2] += text.getHeight();
				break;
			case Middle:
				y = (int) (boundingBox[1] + boundingBox[3] / 2 - text.getHeight() / 2);
				if(text.getHeight() > boundingBox[3]){
					paddingAroundAxis[0] += (text.getHeight() - boundingBox[3]) / 2;
					paddingAroundAxis[2] += (text.getHeight() - boundingBox[3]) / 2;
				}
				break;
			case Top:
				y = (int) (boundingBox[1] - text.getHeight());
				paddingAroundAxis[0] += text.getHeight();
				break;
			}
		}
		else { // vertical Axis
			switch(axis.axisLabel.getHorizontalPosition()){
			case Auto:
				//left
				if (axis.getAxisPosition() == AxisPosition.Maximum && perpAxis.getAxisDirection() == AxisDirection.Horizontal_Ascending_To_Left ||
				axis.getAxisPosition() == AxisPosition.Minimum && perpAxis.getAxisDirection() == AxisDirection.Horizontal_Ascending_To_Right) {
					x = (int) (boundingBox[0] - text.getWidth());
					paddingAroundAxis[3] += text.getWidth();
				}
				else{
					x = (int) (boundingBox[0] + boundingBox[2]);
					paddingAroundAxis[1] += text.getWidth();
				}
				break;
			case Middle:
				x = (int) (boundingBox[0] + boundingBox[2] / 2 - text.getWidth() / 2);
				if(text.getWidth() > boundingBox[2]){
					paddingAroundAxis[1] += (text.getWidth() - boundingBox[2]) / 2;
					paddingAroundAxis[3] += (text.getWidth() - boundingBox[2]) / 2;
				}
				break;
			case Left:
				x = (int) (boundingBox[0] - text.getWidth());
				paddingAroundAxis[3] += text.getWidth();
				break;
			case Right:
				x = (int) (boundingBox[0] + boundingBox[2]);
				paddingAroundAxis[1] += text.getWidth();
				break;
			}
			switch(axis.axisLabel.getVerticalPosition()){
			case Auto:
			case Middle:
				y = (int) (boundingBox[1] + boundingBox[3] / 2 - text.getHeight() / 2);
				break;				
			case Bottom:
				y = (int) (boundingBox[1] + boundingBox[3] - text.getHeight());
				break;
			case Top:
				y = (int) boundingBox[1];
				break;
			}
		}
		if(display){
			axis.axisLabel.setLeft(x);
			axis.axisLabel.setTop(y);
			labelFactory.addAndDisplayStyledLabel(axis.axisLabel);
			labelsPerAxis.get(axis).add(axis.axisLabel);
		}
	}


	private void removeAllGOAndLabelRelatedToAxis(Axis axis) {
		if (gosPerAxis.get(axis) != null) {
			gosPerAxis.get(axis).removeAllGraphicalObjects();
		}
		if(labelsPerAxis.get(axis) == null) {
			return;
		}
		for(StyledLabel l : labelsPerAxis.get(axis)){
			labelFactory.removeStyledLabel(l);
		}
		labelsPerAxis.get(axis).clear();
	}

	public double[] getActualModulPaddingForAxis(Axis axis){
		Axis perpAxis = axis.getPerpendicularAxis();

		double[] paddingAroundAxis = paddingAroundAxes.get(axis);
		if(paddingAroundAxis == null){
			return new double[]{0,0,0,0};
		}
		double top = 0;
		double right = 0;
		double bottom = 0;
		double left = 0;
		boolean visible = true;
		if(axis.isHorizontal()){
			if(axis.axisPosition == AxisPosition.Fixed || axis.axisPosition == AxisPosition.Fixed_Dock_If_Not_Visible){
				double axisLinePos = axis.getModulToAlign().getCartesianAssist().modelToCanvasVertical(axis.fixedPosition);
				if(axisLinePos < axis.modulToAlign.getTopPadding()){
					if(axis.axisPosition == AxisPosition.Fixed_Dock_If_Not_Visible){
						top = paddingAroundAxis[0];
					}
					visible = false;
				}
				else if(axisLinePos > axis.modulToAlign.getCartesianAssist().getHeight() + axis.modulToAlign.getTopPadding()){
					if(axis.axisPosition == AxisPosition.Fixed_Dock_If_Not_Visible){
						bottom = paddingAroundAxis[2];
					}
					visible = false;
				}
				else{
					top = Math.max(0, axis.modulToAlign.getTopPadding() - axisLinePos + paddingAroundAxis[0]);
					bottom = Math.max(0, axisLinePos + paddingAroundAxis[2] - axis.modulToAlign.getCartesianAssist().getHeight() - axis.modulToAlign.getTopPadding()); 
				}
			}
			//bottom
			else if( (axis.axisPosition == AxisPosition.Maximum && perpAxis.getAxisDirection() == AxisDirection.Vertical_Ascending_To_Bottom) ||
					(axis.axisPosition == AxisPosition.Minimum && perpAxis.getAxisDirection() == AxisDirection.Vertical_Ascending_To_Top) ){
				bottom = paddingAroundAxis[2];
			}
			//top
			else if( (axis.axisPosition == AxisPosition.Maximum && perpAxis.getAxisDirection() == AxisDirection.Vertical_Ascending_To_Top) ||
					(axis.axisPosition == AxisPosition.Minimum && perpAxis.getAxisDirection() == AxisDirection.Vertical_Ascending_To_Bottom) ){
				top = paddingAroundAxis[0];
			}
			if(visible){
				left = paddingAroundAxis[3];
				right = paddingAroundAxis[1];
			}
		}
		else{
			if(axis.axisPosition == AxisPosition.Fixed || axis.axisPosition == AxisPosition.Fixed_Dock_If_Not_Visible){
				double axisLinePos = axis.getModulToAlign().getCartesianAssist().modelToCanvasHorizontal(axis.fixedPosition);
				if(axisLinePos < axis.modulToAlign.getLeftPadding()){
					if(axis.axisPosition == AxisPosition.Fixed_Dock_If_Not_Visible){
						left = paddingAroundAxis[3];
					}
					visible = false;
				}
				else if(axisLinePos > axis.modulToAlign.getCartesianAssist().getWidth() + axis.modulToAlign.getLeftPadding()){
					if(axis.axisPosition == AxisPosition.Fixed_Dock_If_Not_Visible){
						right = paddingAroundAxis[1];
					}
					visible = false;
				}
				else{
					left = Math.max(0, axis.modulToAlign.getLeftPadding() - axisLinePos + paddingAroundAxis[3]);
					right = Math.max(0, axisLinePos + paddingAroundAxis[1] - axis.modulToAlign.getCartesianAssist().getWidth() - axis.modulToAlign.getLeftPadding()); 
				}
			}
			//left
			else if( (axis.axisPosition == AxisPosition.Maximum && perpAxis.getAxisDirection() == AxisDirection.Horizontal_Ascending_To_Left) ||
					(axis.axisPosition == AxisPosition.Minimum && perpAxis.getAxisDirection() == AxisDirection.Horizontal_Ascending_To_Right) ){
				left = paddingAroundAxis[3];
			}
			//right
			else if( (axis.axisPosition == AxisPosition.Maximum && perpAxis.getAxisDirection() == AxisDirection.Horizontal_Ascending_To_Right) ||
					(axis.axisPosition == AxisPosition.Minimum && perpAxis.getAxisDirection() == AxisDirection.Horizontal_Ascending_To_Left) ){
				right = paddingAroundAxis[1];
			}
			if(visible){
				top = paddingAroundAxis[0];
				bottom = paddingAroundAxis[2];
			}
		}

		return new double[] {top, right, bottom, left};
	}

	/**
	 * Applies the Defaults or the axis' defaultTick to all ticks on the axis
	 * @param axis
	 */
	private void createDefaultTickAndLabelForAxis(Axis axis) {
		boolean first = true;
		for(Tick t : axis.getTicks()){

			//use given defaults
			if(axis.defaultTick != null) {
				if(axis.defaultTick.formatString != null){
					t.setFormatString(axis.defaultTick.formatString);
				}
				t.setTextContainer(axis.defaultTick.textContainer);
				t.getText().setTextProperties(axis.defaultTick.getText().getTextProperties());
				if(first && !axis.displayFirstTick){
					t.setTickLine(null);
					first = false;
					continue;
				}
				t.setGridLine(axis.defaultTick.gridLine);
				t.setTickLine(axis.defaultTick.tickLine);
				t.setTickPosition(axis.defaultTick.tickPosition);
				t.setTickLength(axis.defaultTick.tickLength);				
			}
			// use default defaults :)
			else{
				if(axis.autoCreateGrids 	
						//						&& t.getPosition() != axis.getMin() && t.getPosition() != axis.getMax()
						){
					t.setGridLine(Defaults.gridLine());
				}
				t.setText(new com.inepex.inechart.shared.label.Text(t.position + ""));
				t.setTickLength(Defaults.tickLength);
			}

			if(axis.tickCustomizer != null){
				axis.tickCustomizer.setTick(t, axis);
			}
		}
	}
}

